package com.example.screenplayer;

import android.util.Log;
import android.view.Surface;

import java.net.URI;
import java.net.URISyntaxException;

public class SocketClientManager implements ScreenSocketClient.SocketCallback {

private static final int SOCKET_PORT = 12345;

private ScreenDecoder mScreenDecoder;
private ScreenSocketClient mSocketClient;

public void start(Surface surface, String ip) {
        mScreenDecoder = new ScreenDecoder();
        mScreenDecoder.startDecode(surface);
        try {
                // 使用传入的 IP 地址拼接 WebSocket URI
                URI uri = new URI("ws://" + ip + ":" + SOCKET_PORT); // 拼接 IP 地址与端口
                mSocketClient = new ScreenSocketClient(this, uri);
                mSocketClient.connect();
        } catch (URISyntaxException e) {
                e.printStackTrace();
        }
}


public void stop () {
        if (mSocketClient != null) {
                mSocketClient.close();
        }
        if (mScreenDecoder != null) {
                mScreenDecoder.stopDecode();
        }
}

@Override
public void onReceiveData (byte[] data) {
        if (mScreenDecoder != null) {
                mScreenDecoder.decodeData(data);
        }
}
}
